home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / RMI_OS / RMI-PREB / EXAMPLES / STOCK / STOCKAPP.JAV < prev    next >
Encoding:
Text File  |  1996-11-08  |  10.2 KB  |  348 lines

  1. /*
  2.  * %W% %E%
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_pre-beta
  20.  */
  21. package examples.stock;
  22.  
  23. import java.applet.Applet;
  24. import java.awt.*;
  25. import java.net.URL;
  26. import java.rmi.*;
  27. import java.rmi.server.*;
  28. import java.util.*;
  29.  
  30. /**
  31.  * The StockApplet exports a remote object, and contacts the StockWatch
  32.  * server to register interest in receiving stock updates.  The applet
  33.  * displays the updates in a graph format.
  34.  */
  35. public class StockApplet extends Applet
  36.     implements StockNotify, java.io.Serializable
  37. {
  38.     /** maximum number of updates displayed */
  39.     static final int MAX_UPDATES = 34;
  40.     /** maximum width of panel */
  41.     private static final int width  = 500;
  42.     /** maximum height of panel */
  43.     private static final int height = 350;
  44.  
  45.     /** vector containing time of each update */
  46.     private Vector time = new Vector(MAX_UPDATES);
  47.     /** table mapping stock names to stock data */
  48.     private Hashtable stockTable = new Hashtable();
  49.     /** reference to StockWatch server */
  50.     private StockWatch stockWatch = null;
  51.     
  52.     private String name[] = { "Sun", "HP", "Microsoft", "DEC","Novell",
  53.                   "IBM","Apple","Netscape","Borland","SGI"};
  54.     private Color color[]= { new Color(205,92,92),
  55.                  Color.orange,
  56.                  new Color(220,220,90),
  57.                  new Color(85, 107, 47),
  58.                  Color.blue,
  59.                  new Color(160,32,240),
  60.                  new Color(238, 130, 238),
  61.                  Color.black,
  62.                  new Color(205,120,92),
  63.                  new Color(0,100,0) };
  64.     
  65.     /**
  66.      * Notification of stock updates for a particular time.
  67.      * @param date the time of the stock update
  68.      * @param stocks an array containing the stocks for which the
  69.      * object has registered interest.
  70.      */
  71.     public void update (Date date, Stock[] stock) 
  72.     {
  73.     System.out.println("StockApplet.update: " + date);
  74.     // record date
  75.     if (time.size() == MAX_UPDATES) { 
  76.         time.removeElementAt(0);
  77.     }
  78.     time.addElement(date);
  79.  
  80.     // record individual stock updates
  81.     int numUpdates = time.size();
  82.     for (int i=0; i<stock.length; i++) {
  83.         StockData data = (StockData)stockTable.get(stock[i].symbol);
  84.         if (data != null) {
  85.         data.update(stock[i], numUpdates);
  86.         }
  87.     }
  88.     repaint();
  89.     }
  90.  
  91.     /**
  92.      * Initialize applet: export the applet as remote object that gets
  93.      * notified of stock updates.
  94.      */
  95.     public void init() 
  96.     {
  97.     try {
  98.         // export the applet as a remote object
  99.         System.out.println("StockApplet.init: exporting remote object");
  100.         UnicastRemoteObject.exportObject(this);
  101.         
  102.         // lookup StockWatch server
  103.         URL base = getDocumentBase();
  104.         String serverName = "//" + base.getHost() + ":" +
  105.         getParameter("registryPort") + "/example.stock.StockServer";
  106.         
  107.         System.out.println("StockApplet.init: looking up server");
  108.         stockWatch = (StockWatch)Naming.lookup(serverName);
  109.  
  110.         // register interest in receiving stock updates
  111.         for (int i=0; i<name.length; i++) {
  112.         System.out.println("StockApplet.init: watch stock " + name[i]);
  113.         stockWatch.watch(name[i], this);
  114.         stockTable.put(name[i], new StockData(name[i], color[i]));
  115.         }
  116.         System.out.println("StockApplet.init: done registering stocks");
  117.         
  118.     } catch (Exception e) {
  119.         // fatal error
  120.         System.out.println("got exception: " + e.getMessage());
  121.         e.printStackTrace();
  122.         return;
  123.     }
  124.  
  125.         setLayout(null);
  126.  
  127.     // draw checkboxes
  128.     Enumeration enum =  stockTable.elements();
  129.     int i=0; 
  130.     while (enum.hasMoreElements()) {
  131.         StockData data = (StockData)enum.nextElement();
  132.             SensitiveCheckbox cb = new SensitiveCheckbox (data, this);
  133.             data.cb = cb;
  134.             add(cb);
  135.             cb.setState(data.displayed);
  136.             cb.reshape(10,i++*25+35,110,18);
  137.         }
  138.     }
  139.  
  140.     /**
  141.      * Called when applet is destroyed; the applet cancels all
  142.      * requests for stock updates.
  143.      */
  144.     public void destroy() 
  145.     {
  146.     // cancel request for stock updates
  147.     if (stockWatch != null) {
  148.         try {
  149.         stockWatch.cancelAll(this);
  150.         } catch (Exception e) {
  151.         // eat exception
  152.         }
  153.     }
  154.     }
  155.  
  156.     /**
  157.      * Called to repaint the panel.
  158.      */
  159.     public void paint(Graphics g) {
  160.  
  161.         // draw black boarder
  162.         g.setColor(Color.black);
  163.         g.drawRect(0,0,width-1,height-1);
  164.  
  165.         float miny = 0.0f;
  166.         float maxy = 75.0f;
  167.  
  168.     // draw all stock data
  169.     Enumeration enum = stockTable.elements();
  170.     while (enum.hasMoreElements()) {
  171.         StockData data = (StockData)enum.nextElement();
  172.  
  173.             int size;
  174.             Stock[] updates;
  175.             synchronized (data.updates) {
  176.                 size = data.updates.size();
  177.                 updates = new Stock[size];
  178.                 data.updates.copyInto(updates);
  179.             }
  180.         
  181.             g.setColor(data.color);
  182.  
  183.             if (data.displayed) {
  184.         // draw box around checkbox if mouse is over it
  185.                 if (data.cb != null && data.cb.haveMouse()) {
  186.                     Point p = data.cb.location();
  187.                     Dimension d = data.cb.size();
  188.  
  189.                     g.drawRect(p.x-1, p.y-1, d.width+4, d.height+4);
  190.                     g.drawRect(p.x-2, p.y-2, d.width+4, d.height+4);
  191.             // point to graph for stock
  192.                     if (size > 0)
  193.                         g.drawLine(p.x+d.width+2,p.y+10,150,
  194.                                    scale(updates[0].current));
  195.                 }
  196.         // draw graph of updates for this stock
  197.                 int x = 150, inc = 10;
  198.                 for (int i = 0; i < size; i++) {
  199.                     if (updates[i] != null) {
  200.                         g.drawRect(x-1,scale(updates[i].current)-1,3,3);
  201.                         if ((i < size - 1) && updates[i + 1] != null) {
  202.                             int x2 = x + inc;
  203.                             g.drawLine(x, scale(updates[i].current), x2,
  204.                                        scale(updates[i + 1].current));
  205.                         }
  206.                     }
  207.             x += inc;
  208.         }
  209.         }
  210.         }
  211.     }
  212.  
  213.     /**
  214.      * Used to scale y-values.
  215.      */
  216.     int scale(float y) {
  217.         return height - (int) (y*5+.5);
  218.     }
  219.  
  220.     /**
  221.      * Make sure that mouseHere is set properly (fix for windows
  222.      * display problems).
  223.      */
  224.     void setMouseHere(boolean display) 
  225.     {
  226.     Enumeration enum = stockTable.elements();
  227.     while (enum.hasMoreElements()) {
  228.         StockData data = (StockData)enum.nextElement();
  229.         data.cb.mouseHere = display;
  230.     }
  231.     }
  232. }
  233.  
  234. /**
  235.  * StockData contains stock updates and display information.
  236.  */
  237. class StockData {
  238.     public String name;
  239.     public Vector updates;
  240.     public Color color;
  241.     public boolean displayed;
  242.     public SensitiveCheckbox cb;
  243.  
  244.     public StockData(String name, Color cl) {
  245.         this.name = name;
  246.         this.color = cl;
  247.     this.updates = new Vector(StockApplet.MAX_UPDATES);
  248.         displayed = true;
  249.     }
  250.  
  251.     /**
  252.      * Update stock.
  253.      */
  254.     void update(Stock stock, int numUpdates) 
  255.     {
  256.         synchronized (updates) {
  257.             if (updates.size() == StockApplet.MAX_UPDATES) {
  258.                 updates.removeElementAt(0);
  259.             }
  260.             // If this stock has not received updates for previous timeslices,
  261.             // fill them in with the current update value as well.
  262.             if (updates.size() < numUpdates) {
  263.                 for (int i=updates.size(); i<numUpdates-1; i++) {
  264.                     updates.addElement(stock);
  265.                 }
  266.             }
  267.             updates.addElement(stock);
  268.         }
  269.     }
  270. }
  271.  
  272. /**
  273.  * A mouse-sensitive checkbox that records whether the mouse is over
  274.  * the checkbox.
  275.  */
  276. class SensitiveCheckbox extends Canvas {
  277.     StockData data;
  278.     boolean state = true;
  279.     StockApplet panel;
  280.     boolean mouseHere = false;
  281.  
  282.     public boolean haveMouse() {
  283.         return mouseHere;
  284.     }
  285.  
  286.     public SensitiveCheckbox(StockData data, StockApplet p) {
  287.         this.data = data;
  288.         panel = p;
  289.     }
  290.  
  291.     public boolean mouseEnter(Event evt, int x, int y) {
  292.         if (state) {
  293.         panel.setMouseHere(false); // for windows
  294.             mouseHere = true;
  295.             panel.repaint();
  296.         }
  297.         return false;
  298.     }
  299.     public boolean mouseExit(Event evt, int x, int y) {
  300.         if (state) {
  301.             mouseHere = false;
  302.             panel.repaint();
  303.         }
  304.         return false;
  305.     }
  306.  
  307.     public boolean mouseDown(Event evt, int x, int y) {
  308.         if (state)
  309.             state = false;
  310.         else
  311.             state = true;
  312.         mouseHere=state;
  313.         data.displayed = state;
  314.         repaint();
  315.         panel.repaint();
  316.         return true;
  317.     }
  318.  
  319.     public void paint(Graphics g) {
  320.         g.setColor(Color.black);
  321.         g.drawLine(4,4,14,4);
  322.         g.drawLine(4,4,4,14);
  323.         g.setColor(Color.gray);
  324.         g.drawLine(5,14,14,14);
  325.         g.drawLine(14,5,14,14);
  326.         g.setColor(data.color);
  327.         g.fillRect(5,5,8,8);
  328.         g.setColor(data.color);
  329.         g.drawString(data.name, 17,15);
  330.         g.setColor(Color.black);
  331.         if (state) {
  332.             if (data.color == Color.black)
  333.                 g.setColor(Color.gray);
  334.             if (data.color == Color.blue)
  335.                 g.setColor(Color.gray);
  336.             g.drawLine(5,5,13,13);
  337.             g.drawLine(5,6,12,13);
  338.             g.drawLine(13,5,5,13);
  339.             g.drawLine(13,6,6,13);
  340.         }
  341.     }
  342.  
  343.     public void setState(boolean s) {
  344.         state = s;
  345.         repaint();
  346.     }
  347. }
  348.